library(tidyverse)
library(fixest)
library(showtext)
library(vtable)
library(writexl)
library(haven)
library(gridExtra)

setwd('D:\\WorkingPaper-Series\\Township_Light\\Replication_Files\\workdata')

##################################
########## Figure C6 #############
##################################

after16 <- read_delim('twfe_est_after16_compare.csv')

after16 <- after16 %>% mutate(upper_ci = coef + 1.96*std_err, lower_ci = coef - 1.96*std_err)

after16 %>% filter(period<=4&period>=-5) %>% 
  ggplot(aes(period,coef,color = type,group = type)) +
  geom_hline(yintercept = 0,color = 'black', lwd = 0.5, lty = 2)+
  geom_vline(xintercept = -1, color = 'grey50', lwd = 0.5, lty = 4)+
  geom_rect(aes(xmin = -4.5, xmax = -1.5, ymin = -Inf, ymax = Inf),
            fill = "grey80", alpha = 0.01,show.legend = F,inherit.aes = F)+
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), width=.2, lwd = .3,position=position_dodge(0.3))+
  geom_point(aes(shape = type, color = type, fill = type),position=position_dodge(0.3)) +
  theme_bw()+
  labs(y = '', x = 'Relative Time') +
  theme(legend.position = c(0.15,0.15),
        legend.background = element_rect(color = 'black', size = 0.1,linewidth = 0.1, fill = 'white'),
        legend.margin = margin(t = -0.45, r = 0.1, b = 0, l = 0.1, unit = "cm"),
        text = element_text(family = 'serif',size = 10),
        axis.text =  element_text(family = 'serif',size = 8))+
  labs(y = '', x = 'Relative Time') +
  scale_x_continuous(breaks = c(seq(-5,4,1)), labels = c('-5+','-4','-3','-2','-1','0','1','2','3','4'))+
  scale_y_continuous(limits = c(-0.3,0.1))+
  scale_color_manual(values = c('after16' = '#D80032', 'baseline' = 'black'),
                     labels = c('baseline' = 'Full Sample', 'after16' = 'Cohort >= 2016')) +
  scale_fill_manual(values = c('after16' = '#D80032', 'baseline' = 'black'),
                    labels = c('baseline' = 'Full Sample', 'after16' = 'Cohort >= 2016')) +
  scale_shape_manual(values = c('after16' = 21, 'baseline' = 23),
                     labels = c('baseline' = 'Full Sample', 'after16' = 'Cohort >= 2016')) +
  guides(colour = guide_legend(""),fill = guide_legend(""),shape = guide_legend(""))